package com.gscitysfy.cus.drain.modules.resource.convert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cloudfrmwk.portal.feign.DistrictService;
import com.gscitysfy.cus.drain.modules.command.entity.DangerSituationManage;
import com.gscitysfy.cus.drain.modules.command.entity.InstructCar;
import com.gscitysfy.cus.drain.modules.command.mapper.InstructCarMapper;
import com.gscitysfy.cus.drain.modules.resource.dto.ResourceCarDto;
import com.gscitysfy.cus.drain.modules.resource.entity.ResourceCar;
import com.gscitysfy.cus.drain.utils.CoordinateUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author jiang_wen
 * @Date 2022/10/23 15:42
 * @Description TODO
 */
@AllArgsConstructor
@Component
public class ResourceCarConvert {

    private final DistrictService districtService;

    private final InstructCarMapper instructCarMapper;

    public ResourceCar toEntity(ResourceCar resourceCar, ResourceCarDto resourceCarDto) {
        resourceCar.setCarName(resourceCarDto.getCarName());
        resourceCar.setCarPlate(resourceCarDto.getCarPlate());
        resourceCar.setResponser(resourceCarDto.getResponser());
        resourceCar.setAddress(resourceCarDto.getAddress());
        resourceCar.setLgtd(resourceCarDto.getLgtd());
        resourceCar.setLttd(resourceCarDto.getLttd());
        resourceCar.setPhone(resourceCarDto.getPhone());
        resourceCar.setRemark(resourceCarDto.getRemark());
        resourceCar.setSimNo(resourceCarDto.getSimNo());
        resourceCar.setGps(resourceCarDto.getGps());
        resourceCar.setGpsCode(resourceCarDto.getGpsCode());
        resourceCar.setCmRegflag(resourceCarDto.getCmRegflag());
        return resourceCar;
    }

    public ResourceCarDto toDto(ResourceCar resourceCar, DangerSituationManage dangerSituationManage) {
        ResourceCarDto resourceCarDto = new ResourceCarDto();
        resourceCarDto.setCmKid(resourceCar.getCmKid());
        resourceCarDto.setCarName(resourceCar.getCarName());
        resourceCarDto.setPhone(resourceCar.getPhone());
        resourceCarDto.setCarPlate(resourceCar.getCarPlate());
        //计算经纬度
        BigDecimal startXPoint = resourceCar.getLgtd();
        BigDecimal startYPoint = resourceCar.getLttd();
        BigDecimal endXPoint = dangerSituationManage.getLongitude();
        BigDecimal endYPoint = dangerSituationManage.getLatitude();
        BigDecimal distance = CoordinateUtil.getDistance(startXPoint, startYPoint, endXPoint, endYPoint);
        resourceCarDto.setDistance(distance.doubleValue());

        LambdaQueryWrapper<InstructCar> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(InstructCar::getCarId, resourceCar.getCmKid());
        queryWrapper.eq(InstructCar::getStatus, "ACCEPT");
        queryWrapper.eq(InstructCar::getCmUsable, false);
        List<InstructCar> instructCarList = instructCarMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(instructCarList)) {
            resourceCarDto.setAssignedFlag(true);
        }else {
            resourceCarDto.setAssignedFlag(false);
        }
        return resourceCarDto;
    }


    public ResourceCarDto toDto(ResourceCar resourceCar) {
        ResourceCarDto resourceCarDto = new ResourceCarDto();
        resourceCarDto.setCmKid(resourceCar.getCmKid());
        resourceCarDto.setCarName(resourceCar.getCarName());
        resourceCarDto.setCarPlate(resourceCar.getCarPlate());
        resourceCarDto.setResponser(resourceCar.getResponser());
        resourceCarDto.setAddress(resourceCar.getAddress());
        resourceCarDto.setLgtd(resourceCar.getLgtd());
        resourceCarDto.setLttd(resourceCar.getLttd());
        resourceCarDto.setPhone(resourceCar.getPhone());
        resourceCarDto.setRemark(resourceCar.getRemark());
        resourceCarDto.setSimNo(resourceCar.getSimNo());
        resourceCarDto.setGps(resourceCar.getGps());
        resourceCarDto.setGpsCode(resourceCar.getGpsCode());
        resourceCarDto.setCmRegflag(resourceCar.getCmRegflag());
        if (Objects.nonNull(resourceCar.getCmRegflag())) {
            Result<String> result = districtService.getDistrictNameByCode(String.valueOf(resourceCar.getCmRegflag()));
            if (result.getStatus() == 200) {
                resourceCarDto.setCmRegflagName(result.getData());
            }
        }
        resourceCarDto.setAssignedFlag(resourceCar.getAssignedFlag());
        return resourceCarDto;
    }

    public List<ResourceCarDto> toDtoList(List<ResourceCar> resourceCarList) {
        if (CollectionUtils.isEmpty(resourceCarList)) {
            return Collections.emptyList();
        }
        return resourceCarList.stream().map(this::toDto).collect(Collectors.toList());
    }

}
